/*  
 * Copyright 2007 MBARI 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package org.mbari.timeline.swing;

import static org.mbari.timeline.swing.TableDisplayNames.duration1;
import static org.mbari.timeline.swing.TableDisplayNames.duration2;
import static org.mbari.timeline.swing.TableDisplayNames.name;
import static org.mbari.timeline.swing.TableDisplayNames.time;

import java.util.Comparator;

import org.mbari.timeline.model.MultipartEvent;

import ca.odell.glazedlists.gui.AdvancedTableFormat;

/**
* A class to display a two-part MultiPartEvent in table format. The columns are:
* name, time, duration 1, duration 2.
* To add a new column, add an enum to TableDisplayNames, insert into the tableOrder array
* and add logic to the getColumnValue method
* 
* NOTE: This class could be easily modified to take a MultiPartEvent with an arbitrary
* number of durations (not just two). This could be done by pulling the duration1 & 2 
* out of the tableOrder and assuming that all columns past tableOrder.length maps to
* the index of Durations in the MultiPartEvent
* 
* @author Andrew Chase
*/
public class TwoPartEventTableFormat implements AdvancedTableFormat<MultipartEvent> {
	
	final TableDisplayNames[] tableOrder = {name, time, duration1, duration2}; 

	public Class getColumnClass(int index) {
		return tableOrder[index].clazz;
	}

	public Comparator getColumnComparator(int index) {
		return tableOrder[index].comparator;
	}

	public int getColumnCount() {
		return tableOrder.length;
	}

	public String getColumnName(int column) {
		return tableOrder[column].text;
	}

	public Object getColumnValue(MultipartEvent event, int column) {
		TableDisplayNames columnInfo = tableOrder[column];
		if(columnInfo == name) return event.getName();
		if(columnInfo == time) return event.getTime();
		if(columnInfo == duration1) return event.getDurationPart(0);
		if(columnInfo == duration2) return event.getDurationPart(1);
		throw new IllegalStateException();
	}

}